#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 2e5 + 10, INF = 0x3f3f3f3f;

int n, m;
vector<pair<int, int>> g[N];
bool vis[N];

void dfs(int x, int ans, int k)
{
    vis[x] = 1;
    for (auto [y, w] : g[x])
        if (vis[y] == 0 && (ans | w) < (ans | (1 << k)))
            dfs(y, ans, k);
}

void solve()
{
    cin >> n >> m;
    for (int i = 1;i <= m;i++) {
        int x, y, w;cin >> x >> y >> w;
        g[x].push_back({ y,w });
        g[y].push_back({ x,w });
    }

    int ans = 0;
    for (int k = 29;k >= 0;k--) {
        memset(vis, 0, sizeof vis);
        dfs(1, ans, k);
        if (vis[n] == 0) ans |= 1 << k;
    }
    cout << ans;
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}